using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TeamTime.Classes;
using StillVoiceCommon;
namespace TeamTime
{
    public partial class PersonsForm : System.Web.UI.Page //TeamTime.Classes.CompressedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Companies companies = new Companies();
                this.Page.DataBind();
                // this.txtFromDate.Text = DateTime.Now.AddDays(-7).ToString("yyyy/MM/dd"); //default to 1 weeks worth of Batches
                /*GridView1.Attributes.Add("SortExpression", "PersonName");*/
                Lookups lookups = new Lookups();
                Functions.dropDownListPopulate(this.ddlOfficeID, lookups.getOffices(true), "ID", "Description");
                ViewState["SortExpression"] = "FullName"; ViewState["SortDirection"] = "asc";
               // GridView1.Attributes.Add("SortExpression", "Surname");
                EnableControls();
                btnSearch_Click(sender, e);
                this.Form.DefaultFocus = this.txtSearchFor.ClientID;
                this.Form.DefaultButton = this.btnSearch.UniqueID;
                if (Functions.IsNumeric(Request.QueryString["ID"]))
                {
                    this.PersonFormControl1.PersonID = Convert.ToInt32( Request.QueryString["ID"]);
                    //LoadDetailsForm(this.txtID.Text);
                    this.txtSearchFor.Text = this.PersonFormControl1.PersonSurname;
                    popUp1.Show();
                }
            }

        }

        private void EnableControls()
        {

            Functions.enableChildControls(this.Form);
            StaffMember staffMember = new StaffMember();
            staffMember.setOfficeDropDown(ddlOfficeID, Convert.ToInt32(Session["UserID"]));

            /*
            this.trID.Visible = true;
            this.txtID.Enabled = false;
            this.txtUserNameModifiedBy.Enabled = false;
            this.txtRecordDate.Enabled = false;
            this.ddlPersonServiceID.Visible = true;
            this.ddlCompanyID.Enabled = false;
            */
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        private void BindGrid()
        {
            Persons persons = new Persons();
            this.GridView1.DataSource = persons.searchPersons(this.txtSearchFor.Text, this.txtCompanyNameSearch.Text, false, ViewState["SortExpression"] + " " + ViewState["SortDirection"], Convert.ToInt16(this.ddlOfficeID.SelectedValue)); 
            this.GridView1.DataBind();
            //this.GridView1.Columns[7].Visible = this.chkShowNotes.Checked; //Show/Hide the notes col.
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            PersonFormControl1.setForAdding();
            popUp1.Show();
        }

        protected void PersonFormControl1_SaveClicked(object sender, EventArgs e)
        {
            popUp1.Show();
            BindGrid();
        }
        protected void PersonFormControl1_LoadDupClicked(object sender, EventArgs e)
        {
            popUp1.Show();
        }


        protected void PersonFormControl1_ListsBuilt(object sender, EventArgs e)
        {
            popUp1.Show();
        }
        protected void PersonFormControl1_MembershipChanged(object sender, EventArgs e)
        {
            popUp1.Show();
            BindGrid();
        }
        //Grid Methods
        //protected void GridView_Sort(object source, DataGridSortCommandEventArgs e)
        //{

        //    GridView1.Attributes.Add("SortExpression", e.SortExpression);
        //    BindGrid();
        //}
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string id = e.CommandArgument.ToString();
                PersonFormControl1.PersonID = Convert.ToInt32(id);
                popUp1.Show();
            }
            else if (e.CommandName == "Delete")
            {
                Person person = new Person();
                string id = e.CommandArgument.ToString();
                person.load(id);
                try
                {
                    person.delete();
                    lblMsg.Text = "The record was deleted";
                }
                catch (Exception  ex)
                {
                    if (ex.Message.Contains("FK_") || ex.Message.Contains("DELETE statement conflicted with the REFERENCE constraint"))
                     
                        lblMsg.Text = "This record cannot be deleted because it has related records.";
                    else
                        throw ex;
                }
                BindGrid();
            }

        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;  // refer to the GridView
            int newPageIndex = 0;

            if (-2 == e.NewPageIndex)
            { // when click the "GO" Button
                TextBox txtNewPageIndex = null;
                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                GridViewRow pagerRow = theGrid.BottomPagerRow; //


                if (null != pagerRow)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                }

                if (null != txtNewPageIndex)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                }
            }
            else
            {  // when click the first, last, previous and next Button
                newPageIndex = e.NewPageIndex;
            }

            // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            // specify the NewPageIndex
            theGrid.PageIndex = newPageIndex;

            // display a new page of data
            // GridView1.EditIndex = -1;
            //GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
            // rebind the control
            // in this case of retrieving the data using the xxxDataSoucr control,
            // just do nothing, because the asp.net engine binds the data automatically
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["SortDirection"] = Functions.getSortDirection(ViewState["SortExpression"].ToString(),
                                                  e.SortExpression, ViewState["SortDirection"].ToString());
            ViewState["SortExpression"] = e.SortExpression;
            BindGrid();
        }


       

    }
   

}



